package DebugStub;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import Enum.XML_ELEMENT;

public class FinestraStubRispostaServer extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3370046770543455265L;
	
	private StubServerResponseControl myControl;
	
	private JComboBox xmlElement;
	private JTextField testo;
	private JButton aggiungi;
	private JTextArea risultato;
	private JButton invia;
	private JButton reset;
	
	public FinestraStubRispostaServer(StubServerResponseControl myControl) {
		super();
		this.myControl = myControl;
		
		this.testo = new JTextField();
		this.aggiungi =new JButton("Aggiungi");
		this.invia = new JButton("Invia");
		this.risultato = new JTextArea();
		this.reset = new JButton("Reset");
		
		risultato.setSize(500, 300);
		risultato.setEditable(false);
		
		Field[] fields = XML_ELEMENT.class.getDeclaredFields();
		
		this.aggiungi.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				aggiungiPressed();
			}
		});
		this.invia.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				inviaPressed();
			}
		});
		this.reset.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				resetPressed();
			}
		});
		this.xmlElement = new JComboBox(fields);		
		
		this.setSize(500, 400);
		this.getContentPane().setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = 2;
		c.gridy = 0;
		this.getContentPane().add(xmlElement,c);
		
		c.gridwidth = 1;
		c.gridy = 1;
		c.weightx = 50;
		this.getContentPane().add(testo,c);
		this.getContentPane().add(aggiungi,c);
		
		c.gridy = 2;
		c.gridwidth = 2;
		c.weighty = 70;
		this.getContentPane().add(risultato,c);
		c.weighty = 0;
		c.gridwidth = 1;
		c.gridy = 3;
		this.getContentPane().add(invia,c);
		this.getContentPane().add(reset,c);
	}
	
	public void setTestoRisultato(String testo) {
		this.risultato.setText(testo);
	}
	
	public String getTesto() {
		return this.testo.getText();
	}
	
	public String getXMLElement() {
		try {
			return ((String)((Field)xmlElement.getSelectedItem()).get(new String()));
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			return null;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public String getRisultato() {
		return this.risultato.getText();
	}
	
	private void resetPressed(){
		myControl.resetModule();
	}
	
	private void inviaPressed(){
		myControl.inviaRichiesta();
	}
	
	private void aggiungiPressed(){
		myControl.aggiungiElemento();
	}
}
